package vip.xiaonuo.biz.modular.comment.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.biz.modular.comment.result.PageComment;
import vip.xiaonuo.biz.modular.order.service.BizTicketOrderService;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.page.CommonPageRequest;
import vip.xiaonuo.biz.modular.comment.entity.BizComment;
import vip.xiaonuo.biz.modular.comment.mapper.BizCommentMapper;
import vip.xiaonuo.biz.modular.comment.param.BizCommentAddParam;
import vip.xiaonuo.biz.modular.comment.param.BizCommentEditParam;
import vip.xiaonuo.biz.modular.comment.param.BizCommentIdParam;
import vip.xiaonuo.biz.modular.comment.param.BizCommentPageParam;
import vip.xiaonuo.biz.modular.comment.service.BizCommentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * 评价留言表Service接口实现类
 *
 * @author gtc
 *
 **/
@Service
public class BizCommentServiceImpl extends ServiceImpl<BizCommentMapper, BizComment> implements BizCommentService {

    private static final Logger log = LoggerFactory.getLogger(BizCommentServiceImpl.class);

    @Autowired
    private BizTicketOrderService bizOrderService;

    @Autowired
    private BizCommentMapper bizCommentMapper;

    @Override
    public Page<PageComment> page(BizCommentPageParam bizCommentPageParam) {
        // 检查分页参数，设置默认值
        if (bizCommentPageParam == null) {
            bizCommentPageParam = new BizCommentPageParam();
        }
        
        // 确保分页参数有效
        if (bizCommentPageParam.getCurrent() == null || bizCommentPageParam.getCurrent() < 1) {
            bizCommentPageParam.setCurrent(1);
        }
        
        if (bizCommentPageParam.getSize() == null || bizCommentPageParam.getSize() < 1) {
            bizCommentPageParam.setSize(10);
        }
        
        try {
            // 创建分页对象
            Page<PageComment> page = new Page<>(bizCommentPageParam.getCurrent(), bizCommentPageParam.getSize());
            
            // 调用Mapper查询
            IPage<PageComment> result = bizCommentMapper.select(page, bizCommentPageParam);
            
            if (result == null) {
                // 如果返回为空，返回空页面
                return new Page<>(bizCommentPageParam.getCurrent(), bizCommentPageParam.getSize(), 0);
            }
            
            return (Page<PageComment>) result;
        } catch (Exception e) {
            log.error("查询评论列表发生异常: {}", e.getMessage(), e);
            // 发生异常时返回空页面，避免抛出异常
            return new Page<>(bizCommentPageParam.getCurrent(), bizCommentPageParam.getSize(), 0);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(BizCommentAddParam bizCommentAddParam) {
        BizComment bizComment = BeanUtil.toBean(bizCommentAddParam, BizComment.class);
        this.save(bizComment);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(BizCommentEditParam bizCommentEditParam) {
        BizComment bizComment = this.queryEntity(bizCommentEditParam.getId());
        BeanUtil.copyProperties(bizCommentEditParam, bizComment);
        this.updateById(bizComment);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<BizCommentIdParam> bizCommentIdParamList) {
        // 执行删除
        this.removeByIds(CollStreamUtil.toList(bizCommentIdParamList, BizCommentIdParam::getId));
    }

    @Override
    public BizComment detail(BizCommentIdParam bizCommentIdParam) {
        return this.queryEntity(bizCommentIdParam.getId());
    }

    @Override
    public BizComment queryEntity(String id) {
        BizComment commentInfo = bizCommentMapper.getCommentInfo(id);

        if(ObjectUtil.isEmpty(commentInfo)) {
            throw new CommonException("评价留言表不存在，id值为：{}", id);
        }
        return commentInfo;
    }

    @Override
    public double getAllSpotsAverageScore() {

        return 3.5;
    }

    @Override
    public int getPriorRatingCount() {
        return 2;
    }

    @Override
    public double getSpotScore(String spotId) {
        Double currentSpotScore =  bizCommentMapper.getSpotScore(spotId);
        return currentSpotScore == null ? 0 : currentSpotScore;
    }

    @Override
    public Integer getRatingCount(String spotId) {
        return bizCommentMapper.getRatingCount(spotId);
    }
}
